package com.bhp.bencaoguoyi.spit.ctl;

import com.bhp.bencaoguoyi.commom.web.AjaxResult;
import com.bhp.bencaoguoyi.spit.entity.Spit;
import com.bhp.bencaoguoyi.spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName SpitCtl
 * @Description TODO
 * @Author 包海鹏
 * @Date 2020/6/18 16:10
 * @Version 1.0
 **/
@RestController
@RequestMapping("/bcgy/spit")
public class SpitCtl {

    @Autowired
    private SpitService spitService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查全部
     */
    @GetMapping
    public AjaxResult findAll() {
        return AjaxResult.success(spitService.findAll());
    }

    /**
     * 根据id查
     */
    @GetMapping("{id}")
    public AjaxResult findById(@PathVariable String id) {
        return AjaxResult.success(spitService.findById(id));
    }

    /**
     * 增加
     */
    @PostMapping
    public AjaxResult add(@RequestBody Spit spit) {
        spitService.add(spit);
        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("{id}")
    public AjaxResult update(@RequestBody Spit spit, @PathVariable String id) {
        spit.setId(id);
        spitService.update(spit);
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("{id}")
    public AjaxResult deleteById(@PathVariable String id) {
        spitService.deleteById(id);
        return AjaxResult.success();
    }

    /**
     * 根据上级id查询吐槽列表（分页）
     */
//    @GetMapping(value = "/comment/{parentId}/{page}/{size}")
//    public AjaxPage<Spit> findByParentId(@PathVariable String parentId, @PathVariable int page, @PathVariable int size) {
//        Page<Spit> pageList = spitService.findByParentid(parentId, page, size);
//        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Spit>(pageList.getTotalElements(), pageList.getContent()));
//    }

    /**
     * 点赞
     */
    @PutMapping("thumbup/{id}")
    public AjaxResult updateThumbup(@PathVariable String id) {
        //判断用户是否点过赞
        String userid = "2023";//后边会修改为当前登录的用户
        if (redisTemplate.opsForValue().get("thumbup_" + userid + "_" + id) != null) {
            return AjaxResult.error("你已经点过赞了");
        }
        spitService.updateThumbup(id);
        redisTemplate.opsForValue().set("thumbup_" + userid + "_" + id, "1");
        return AjaxResult.success();
    }

}
